
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[Scores_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[Scores_Insert]

IF OBJECT_ID(N'[dbo].[Scores_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[Scores_Update]

IF OBJECT_ID(N'[dbo].[Scores_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[Scores_Delete]

IF OBJECT_ID(N'[dbo].[Scores_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[Scores_Select]

--endregion

GO


--region [dbo].[Scores_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Scores_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdScore int = null,
	@Score nvarchar(50) = null,
	@ScoreType nvarchar(50) = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[Scores]
					WHERE
					(
						(@IdScore is null OR [Scores].[IdScore] = @IdScore)
						AND (@Score is null OR [Scores].[Score] like @Score)
						AND (@ScoreType is null OR [Scores].[ScoreType] like @ScoreType)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[Scores].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdScore' and @SortType = 1 THEN [Scores].[IdScore] END ASC,
						CASE WHEN @SortBy = 'IdScore' and @SortType = 0 THEN [Scores].[IdScore] END DESC,
						CASE WHEN @SortBy = 'Score' and @SortType = 1 THEN [Scores].[Score] END ASC,
						CASE WHEN @SortBy = 'Score' and @SortType = 0 THEN [Scores].[Score] END DESC,
						CASE WHEN @SortBy = 'ScoreType' and @SortType = 1 THEN [Scores].[ScoreType] END ASC,
						CASE WHEN @SortBy = 'ScoreType' and @SortType = 0 THEN [Scores].[ScoreType] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[Scores]
			Where 
			(
				(@IdScore is null OR [Scores].[IdScore] = @IdScore)
				AND (@Score is null OR [Scores].[Score] like @Score)
				AND (@ScoreType is null OR [Scores].[ScoreType] like @ScoreType)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[Scores_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[Scores_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Scores_Insert]
	@IdScore int OUTPUT,
	@Score nvarchar(50),
	@ScoreType nvarchar(50)

AS


INSERT INTO [dbo].[Scores] 
(
	[Score],
	[ScoreType]
)
VALUES 
(
	@Score,
	@ScoreType
)

SET @IdScore = SCOPE_IDENTITY()

--end [dbo].[Scores_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[Scores_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Scores_Update]
	@IdScore int,
	@Score nvarchar(50),
	@ScoreType nvarchar(50)
AS


UPDATE [dbo].[Scores] SET
	[Score] = @Score,
	[ScoreType] = @ScoreType
WHERE
	[IdScore] = @IdScore

--end [dbo].[Scores_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[Scores_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Scores_Delete]
	@IdScore int
AS


DELETE FROM [dbo].[Scores]
WHERE
(
	[IdScore] = @IdScore
)

--end [dbo].[Scores_Delete]
--endregion

GO
--=========================================================================================--

